//
//  LoginViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/14.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegistViewController.h"
#import "FindPWDViewController.h"
#import <AVOSCloud.h>
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)LoginView *loginView;
@end

@implementation LoginViewController
- (void)loadView
{
    self.loginView = [[LoginView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.loginView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.loginView.backgroundColor = ColorHex(0x0d1c38);
    //取消按钮方法
    [self.loginView.cancleButton addTarget:self action:@selector(returnBeforeAction) forControlEvents:UIControlEventTouchUpInside];
    //登陆按钮方法
    [self.loginView.loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
    //注册按钮方法
    [self.loginView.registButton addTarget:self action:@selector(registButtonAction) forControlEvents:UIControlEventTouchUpInside];
    //忘记密码按钮方法
    [self.loginView.findPWDButton addTarget:self action:@selector(findPWDButtonAction) forControlEvents:UIControlEventTouchUpInside];

    
}

#pragma mark - 回收键盘方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag >= 10010 && textField.tag <= 10011) {
        UITextField *field = (UITextField *)[self.loginView viewWithTag:textField.tag + 1];
        [field becomeFirstResponder];
    }
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.loginView endEditing:YES];
}
#pragma mark - 取消按钮方法
- (void)returnBeforeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 登陆按钮方法实现
- (void)loginButtonAction
{
    [AVUser logInWithUsernameInBackground:self.loginView.usernameTF.text password:self.loginView.passwordTF.text block:^(AVUser *user, NSError *error) {
        if (user) {

            //登陆成功关闭登陆窗口
            self.loginView.usernameTF.text = nil;
            self.loginView.passwordTF.text = nil;
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            //登陆失败
            self.loginView.usernameTF.text = nil;
            self.loginView.passwordTF.text = nil;
            [self loginFiled];
        }
    }];

}
#pragma mark - 注册按钮方法实现
- (void)registButtonAction
{
    RegistViewController *registVC = [[RegistViewController alloc]init];
    [self presentViewController:registVC animated:YES completion:nil];
}
#pragma mark - 忘记密码按钮方法实现
- (void)findPWDButtonAction
{
    FindPWDViewController *findVC = [[FindPWDViewController alloc]init];
    [self presentViewController:findVC animated:YES completion:nil];
}


#pragma mark - 提示框(登录失败)
- (void)loginFiled
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录失败" message:@"请确认账号和密码是否匹配" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"登录失败" message:@"请确认账号和密码是否匹配" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }
    
}
#pragma mark - 提示框(登录失败)
- (void)loginSuccess
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"登录成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }
    
}

#pragma mark - 延迟2秒方法
- (void)aftertimeAction:(UIAlertView *)sender
{
    [sender dismissWithClickedButtonIndex:0 animated:YES];
    
}




















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
